package com.product.bargain.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.product.bargain.util.JwtUtil;
import com.product.bargain.vo.BargainShopVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.product.bargain.entity.MybargainEntity;
import com.product.bargain.service.MybargainService;
import com.product.common.utils.PageUtils;
import com.product.common.utils.R;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author brh
 * @email sunlightcs@gmail.com
 * @date 2022-03-10 18:46:48
 */
@RestController
@RequestMapping("bargain/mybargain")
public class MybargainController {
    @Autowired
    private MybargainService mybargainService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = mybargainService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		MybargainEntity mybargain = mybargainService.getById(id);

        return R.ok().put("mybargain", mybargain);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MybargainEntity mybargain){
		mybargainService.save(mybargain);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MybargainEntity mybargain){
		mybargainService.updateById(mybargain);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		mybargainService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    //添加到我的砍价中
    @RequestMapping("/addMyBargain")
    public R addMyBargain(@RequestBody BargainShopVo bargainShopVo){
        mybargainService.addMyBargain(bargainShopVo);
       return R.ok();
    }

    //生成链接
    @RequestMapping("/shareBargain")
    public R shareBargain(@RequestParam String uname,@RequestParam Integer id){
        return mybargainService.shareBargain(uname,id);
    }

    //好友访问
    @RequestMapping("/help/{path}/{uname}/{id}")
    public R help(@PathVariable("path") String path,
                  @PathVariable("uname") String uname,
                  @PathVariable("id") Integer id,
                       HttpServletResponse response){
        String s = (String) redisTemplate.opsForValue().get(uname);
        if (path.equals(s)){
            try {
                redisTemplate.opsForValue().set(path,id.toString());
                response.sendRedirect("http://127.0.0.1:5500/bargainUserLogin.html?path="+path);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            return R.error("访问错误!");
        }
        return null;
    }

    //用户砍价
    @RequestMapping("/pdUser")
    public R pdUser(@RequestParam String path,
                    @RequestParam String uname,
                    @RequestParam Integer userId){
        //首先根据前台传过来的path 获取redis中的存入的商品id
        String id = (String) redisTemplate.opsForValue().get(path);
        //获取到之后查询砍价信息
        MybargainEntity UserBargain = mybargainService.findByUser(id);
        //判断是否是自己砍价!
        if (uname.equals(UserBargain.getUserName())){//是自己砍价
            return R.error("不可以给自己砍价哦!").put("code","100");
        }else{//好友帮砍价
            //获取商品id
            return mybargainService.friendBargain(id,uname,userId);
        }
    }


}
